package servlet;

import bean.Order;
import dao.entity.OrderDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "orderAddServlet", value = "/oadd")
public class OrderAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取数据
        String oid = (String) request.getParameter("oid");
        String order_time= (String) request.getParameter("order_time");
        String order_dest = (String) request.getParameter("order_dest");
        String order_from = (String) request.getParameter("order_from");
        String mid = (String) request.getParameter("mid");
        String mname = (String) request.getParameter("mname");
        String cid = (String) request.getParameter("cid");
        String cname = (String) request.getParameter("cname");
        String c_phone = (String) request.getParameter("c_phone");
        int o_number = Integer.parseInt( request.getParameter("o_number"));
        String order_type = (String) request.getParameter("order_type");
        String eid = (String) request.getParameter("eid");
        double weight = Double.parseDouble(request.getParameter("tweight")) * Integer.parseInt(request.getParameter("o_number"));
        double tweight = weight;
        String o_status = (String) request.getParameter("o_status");
        //封装数据
        Order o = new Order(oid,order_time,order_dest,order_from,mid,mname,cid,cname,o_number,c_phone,order_type,eid,tweight,o_status);
        //执行插入操作
        //int n = SHelper.addS(student);
        OrderDaoImpl odi = new OrderDaoImpl();
        int n = odi.addOrder(o);
        if(n!=0){
            //插入成功
            request.setAttribute("n",1);
        }else {
            //插入失败
            request.setAttribute("n",0);
        }
        request.getRequestDispatcher("result.jsp").forward(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
